package com.jar.netty_demo.NIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {
    public static void main(String[] args) throws IOException {
        //1、
        SocketChannel socketChannel = SocketChannel.open();
        //2、
        socketChannel.configureBlocking(false);
        //3、
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 7777);
        //4、
        if (!socketChannel.connect(inetSocketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("因为连接需要时间，客户端不会阻塞，可以做其他工作");
            }
        }
        String s = "hello,尚硅谷";
        //warp：根据字符串字节数组大小直接初始化长度
        ByteBuffer buffer = ByteBuffer.wrap(s.getBytes());
        socketChannel.write(buffer);
        System.in.read();

    }
}
